package com.example.ruiji.service.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.dao.mapper.ArticleMapper;
import com.example.ruiji.pojo.Article;
import com.example.ruiji.pojo.Tag;
import com.example.ruiji.pojo.User;
import com.example.ruiji.service.TagService.TagService;
import com.example.ruiji.service.userSerice.UserServiceImpl;
import com.example.ruiji.utils.dos.ArchivedDo;
import com.example.ruiji.utils.vo.Result;
import com.example.ruiji.utils.vo.param.ArticleVo;
import com.example.ruiji.utils.vo.param.PageParam;
import com.example.ruiji.utils.vo.param.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author:BaiYiChen
 * Date:2022/4/19 22:31
 *
 * @author BaiYiChen
 */
@Service
public class ArticleServiceImpl implements ArticleService{
  @Autowired
  private ArticleMapper articleMapper;
  @Autowired
  private TagService tagService;
  @Autowired
  private UserServiceImpl userService;
  public static Long cnt;
  @Override
  public Result listArticle(PageParam param) {
    Page<Article> page = new Page<>(param.getPage(), param.getPageSize());
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.orderByDesc(Article::getCreateDate);
    Page<Article> page1 = articleMapper.selectPage(page, queryWrapper);
    List<Article> records = page1.getRecords();
    List<ArticleVo> voList = articleVoList(records,true,true);
    //ArticleVo需要让前端展示的数据
    return Result.successful(voList);
  }
  
  @Override
  public Result hotArticle(Integer limit) {
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.select(Article::getId,Article::getTitle).orderByDesc(Article::getViewCounts).last("limit  "+limit);
    List<Article> hotArticle = articleMapper.selectList(queryWrapper);
    List<ArticleVo> hotArticleVos = articleVoList(hotArticle, false, false);
    return Result.successful(hotArticleVos);
  }
  @Override
  public Long articleCnt(){
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.select(Article::getId);
    Long count = articleMapper.selectCount(queryWrapper);
    return count;
  }
  
  @Override
  public Result recentlyPublished(Integer limit) {
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.select(Article::getId,Article::getTitle)
            .orderByDesc(Article::getCreateDate)
            .last(" limit "+ limit );
    
    List<Article> articleList = articleMapper.selectList(queryWrapper);
    //只返回给前端的看的数据
    List<ArticleVo> vos = articleVoList(articleList,false,false);
    return Result.successful(vos);
  }
  
  @Override
  public Result articleArch() {
    List<ArchivedDo> arch = articleMapper.articleArch();
    return Result.successful(arch);
  }
  
  @Override
  public ArticleVo findArticleById(Integer id) {
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Article::getId,id);
    Article article = articleMapper.selectOne(queryWrapper);
    
    ArticleVo articleVo = articleVo(article, true, true);
    return articleVo;
  }
  
  @Override
  public Integer insertArticle(Article article) {
    return articleMapper.insertArticle(article);
  }
  
  @Override
  public Result listArticleById(Integer id,PageParam param) {
    Page<Article> page = new Page<>(param.getPage(), param.getPageSize());
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Article::getCategoryId ,id);
    Page<Article> page1 = articleMapper.selectPage(page, queryWrapper);
    List<Article> records = page1.getRecords();
    List<ArticleVo> articleVos = articleVoList(records, true, true);
    
    return Result.successful(articleVos);
  }
  
  @Override
  public Result listArticleByCategory(Integer id) {
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Article::getCategoryId, id);
    Long count = articleMapper.selectCount(queryWrapper);
    return Result.successful(count);
  }
  
  @Override
  public Result listArticleByTag(Integer id,PageParam param) {
    Page page = new Page(param.getPage(), param.getPageSize());
    IPage<Article> iPage = articleMapper.findByPage(page, id);
    List<Article> list = iPage.getRecords();
    cnt=iPage.getTotal();
    List<ArticleVo> vos = articleVoList(list, true, true);
    return Result.successful(vos);
  }
  
  @Override
  public Long listArticleCountByTag(Integer id) {
    Long articleCount = articleMapper.listArticleByTag(id);
    return articleCount;
  }
  
  @Override
  public Result findByDate(PageParam param, String date) {
    Page page = new Page(param.getPage(), param.getPageSize());
    IPage<Article> iPage = articleMapper.findByDate(page, date);
    List<Article> records = iPage.getRecords();
    
    List<ArticleVo> list = articleVoList(records, true, true);
    return Result.successful(list);
  }
  
  @Override
  public Long listArticleCountByDate(String date) {
    Long count = articleMapper.listArticleByDate(date);
    return count;
  }
  
  /**
   * 将数据库查询的数据部分数据返回到前端展示
   * @param articleList
   * @return
   */
  public List<ArticleVo> articleVoList(List<Article> articleList,Boolean isAuthor,Boolean isTag){
    List<ArticleVo> voList = new ArrayList<>();
    for (Article article : articleList) {
      voList.add(articleVo(article,isAuthor,isTag));
    }
    return voList;
  }
  public ArticleVo articleVo(Article article,Boolean isAuthor,Boolean isTag){
    final Long LOWER_TIME=107571L;
    ArticleVo articleVo = new ArticleVo();
    BeanUtils.copyProperties(article, articleVo);
    if (article.getCreateDate()!=null) {
      if ((System.currentTimeMillis()-article.getCreateDate().getTime())<=LOWER_TIME){
        articleVo.setCreateDate("刚刚");
      }else{
        articleVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(article.getCreateDate()));
      }
    }
    
    if (isTag) {
      List<TagVo> ltagVo = tagService.findLtagById(article.getId());
      articleVo.setTags(ltagVo);
    }
    if (isAuthor) {
      User author = userService.findUserById(article.getAuthorId());
      articleVo.setAuthorId(author.getId());
      articleVo.setAuthor(author.getNickname());
      articleVo.setStatus(author.getStatus());
    }
    return articleVo;
  }
}
